﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QLDT
{
    public partial class frm_Retore : DevExpress.XtraEditors.XtraForm
    {
        public frm_Retore()
        {
            InitializeComponent();
        }
        private SqlConnection conn;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";

        private void simpleBt_connect_Click(object sender, EventArgs e)
        {
            try
            {

                connectionString = "Data Source=" + txt_datasource.Text + ";User Id=" + txt_userID.Text + ";Password=" + txt_password.Text + ";";
                conn = new SqlConnection(connectionString);
                conn.Open();
                sql = "EXEC sp_databases";
                command = new SqlCommand(sql, conn);
                reader = command.ExecuteReader();
                cmbdatabase.Items.Clear();
                while (reader.Read())
                {
                    cmbdatabase.Items.Add(reader[0].ToString());
                }
                txt_datasource.Enabled = false;
                txt_password.Enabled = false;
                txt_userID.Enabled = false;
                simpleBt_connect.Enabled = false;
                simpleBt_disconnect.Enabled = true;
                simpleBt_retore.Enabled = true;
                cmbdatabase.Enabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void simpleBt_disconnect_Click(object sender, EventArgs e)
        {
            txt_datasource.Enabled = true;
            txt_userID.Enabled = true;
            txt_password.Enabled = true;
            cmbdatabase.Enabled = false;
            simpleBt_retore.Enabled = false;
        }

        private void frm_Retore_Load(object sender, EventArgs e)
        {
            simpleBt_connect.Enabled = true;
            cmbdatabase.Enabled = true;
            simpleBt_retore.Enabled = false;
        }

        private void simpleBt_Browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.FileName = "";
            open.Filter = "File(*.bak)|*.bak";
            if (open.ShowDialog() == DialogResult.OK)
            {
                txt_path.Text = open.FileName;
                txt_path.Text = "QLDT"; //tên file được hiển thị lên textbox 
            }
        }

        private void simpleBt_retore_Click(object sender, EventArgs e)
        {
            ketnoi();
            string phuchoi = "restore database QLDT from disk = '" + txt_path.Text + "' ";
            SqlCommand cmd = new SqlCommand(phuchoi, conn);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Phuc hoi thanh cong !");
            huyketnoi();
        }

        private void huyketnoi()
        {
            conn.Close();
            conn.Dispose();//giai fong tai nguyen 
            conn = null;//huy doi tương 
        }

        private void ketnoi()
        {
            string kn = @"Server=(local);Database=QLDT;Trusted_Connection=True;";
            try
            {
                conn = new SqlConnection(kn);
                conn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("không thể kết nối tới CSDL!", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}